//
//  MineDataViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/21.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "MineDataViewController.h"
#import "EditProfileViewController.h"
#import "MYMineModle.h"
@interface MineDataViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) MYMineModle * mineModle;
@property (nonatomic ,strong) UITableView * tableView;
@property (nonatomic ,strong) NSMutableDictionary *mineDic;

@end

@implementation MineDataViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self loadMydata];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //[[SDImageCache sharedImageCache] clearDiskOnCompletion:^{
    //}];
    self.title=MyString(@"个人资料");
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainW, MainH-64) style:1];
    self.tableView.backgroundColor=Gray_Color;
    self.tableView.delegate = self;
    self.tableView.separatorColor=SeparatorColor;
    self.tableView.separatorInset=UIEdgeInsetsZero;
    self.tableView.separatorStyle=0;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
	
	UIButton *rightB = [UIButton buttonWithType:UIButtonTypeCustom];
	[rightB setTitle:MyString(@"编辑") forState:UIControlStateNormal];
	[rightB addTarget:self action:@selector(CompileBtnClick) forControlEvents:UIControlEventTouchUpInside];
	[rightB setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	rightB.titleLabel.font = [UIFont systemFontOfSize:14];

	UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithCustomView:rightB];
    self.navigationItem.rightBarButtonItem=item;
}
-(void)CompileBtnClick{
    EditProfileViewController*vc=[[EditProfileViewController alloc]init];
    vc.mineDic=self.mineDic;
    [self.navigationController pushViewController:vc animated:YES];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 6;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 49;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"celll"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"celll"];
    }
    cell.selectionStyle=0;
    cell.textLabel.textColor=TextColor3;
    cell.textLabel.font=FontAndStyle14;
    cell.detailTextLabel.textColor=TextColor6;
    cell.detailTextLabel.font=FontAndStyle14;
    if (indexPath.section==0) {
        cell.textLabel.text=MyString(@"头像");
        UIImageView * iconImg = [[UIImageView alloc] initWithFrame:CGRectMake(MainW-50, 5, 40, 40)];
        [iconImg sd_setImageWithURL:[NSURL URLWithString:self.mineDic[@"avatar"]] placeholderImage:[UIImage imageNamed:@""]];
        [cell addSubview:iconImg];
        ViewRadius(iconImg, 20);
    }else if (indexPath.section==1){
        cell.textLabel.text=MyString(@"性别");
        if ([self.mineDic[@"gender"] intValue]==1) {
            cell.detailTextLabel.text=MyString(@"男");
        }else{
            cell.detailTextLabel.text=MyString(@"女");
        }
    }else if (indexPath.section==2){
        cell.textLabel.text=MyString(@"昵称");
        cell.detailTextLabel.text=self.mineDic[@"nickname"];
    }else if (indexPath.section==3){
        cell.textLabel.text=MyString(@"联系电话");
        cell.detailTextLabel.text=[NSString stringWithFormat:@"%@",self.mineDic[@"contactPhone"]];
    }else if (indexPath.section==4){
        cell.textLabel.text=MyString(@"地区");
        cell.detailTextLabel.text=self.mineDic[@"district"];
    }else if (indexPath.section==5){
        cell.textLabel.text=MyString(@"邀请码");
        NSLog(@"----%@",self.mineDic[@"inviterCode"]);
        NSString *code=[NSString stringWithFormat:@"%@",self.mineDic[@"inviterCode"]];
        if (code.length==0) {
            cell.detailTextLabel.text=@"暂无邀请码";
        }else{
            cell.detailTextLabel.text=[NSString stringWithFormat:@"%@",self.mineDic[@"inviterCode"]];
        }
        
    }
    return cell;
}
-(void)loadMydata{
    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
    dic[@"userId"]=[ZJUserDefaultsTools getCurrentUserID];
    [ self doHttpRequestWithApi:API_USER method:GET dic:dic successBlock:^(NSDictionary *dic) {
        self.mineDic=dic[@"data"];
        [self.tableView reloadData];
    }];

}
@end
